<template>
	<view class="p-x-10 p-t-10">
		<view style="background: #fff;" class="b-r-8 p-10" v-if="type == 1">
			<my-forms ref="form" :formItem="formItem" :model="data" labelWidth="75px" />
		</view>

		<view style="background: #fff;" class="b-r-8 p-10" v-else>
			<my-forms ref="form" :formItem="formItem" :model="data" labelWidth="75px" />
		</view>

		<view class="p-10 button">
			<button class="button-1" @click="fn1" v-if="type == 1">转派</button>
			<view class="flex" v-else-if="type == 2">
				<button class="button-1 w100 m-r-20" @click="fn2">处理中</button>
				<button class="button-2 w100" @click="fn3">处理完成</button>
			</view>
			<button class="button-2" @click="fn3" v-else>处理完成</button>
		</view>
	</view>
</template>

<script setup>
	/**************************************获取部门数据*************************************************/
	import {
		deptFn
	} from "@/hooks/dept.js";

	import date from '@/utils/date'

	const type = ref(),
		data = ref({}),
		deptData = ref([]),
		formItem = computed(() => {
			if (type.value == 1) {
				return [
					// 	{
					// 	label: "操作人",
					// 	prop: "operator",
					// 	required: true,
					// 	disabled: true,
					// }, {
					// 	label: "操作时间",
					// 	prop: "operateTime",
					// 	itemType: "dataTime",
					// 	required: true,
					// 	disabled: true,
					// },
					{
						label: "转派部门",
						prop: "transferredDeptName",
						required: true,
						itemType: "cascader",
						map: {
							text: 'node_text',
							value: 'node_text'
						},
						list: deptData.value
					}, {
						label: "转派原因",
						prop: "transferReason",
						itemType: "textarea",
						autoHeight: true,
						required: true,
					}
				]
			} else {
				return [
					// 	{
					// 	label: "处理人",
					// 	prop: "procesName",
					// 	required: true,
					// 	disabled: true,
					// }, {
					// 	label: "处理时间",
					// 	prop: "procesTime",
					// 	itemType: "dataTime",
					// 	required: true,
					// 	disabled: true,
					// }, 
					{
						label: "处理措施",
						prop: "measure",
						itemType: "textarea",
						count: true,
						required: true,
					}
				]
			}
		})


	// 接收消息详情页数据：
	onLoad(async (options) => {
		type.value = options.type;
		uni.setNavigationBarTitle({
			title: type.value == 1 ? '消息转派' : '消息处理'
		})
		// 转派
		if (type.value == 1) {
			data.value.operator = uni.getStorageSync('userInfo').userName
			data.value.operateTime = date()
			data.value.warnId = options.id
			data.value.originalDeptName = options.oldDeptName
			deptData.value = await deptFn()
		} else {
			// 处理
			data.value.id = options.id
		}
	})

	/**************************************转派、处理 逻辑处理*************************************************/
	import {
		warnTransferRecord,
		handlingWarnInfo,
		handleWarnInfo
	} from "@/api/message";

	const form = ref(null)

	// 转派
	function fn1() {
		form.value.$refs.refMyForm.validate().then(val => {
			warnTransferRecord(data.value).then(res => {
				uni.switchTab({
					url: `/pages/message/index`,
				})
			})
		})
	}

	// 处理中
	function fn2() {
		form.value.$refs.refMyForm.validate().then(val => {
			const option = {
				processingReason: data.value.measure,
				processingName: uni.getStorageSync('userInfo').userName,
				processingTime: date(),
				...data.value
			};
			delete option.measure;

			handlingWarnInfo(option).then(res => {
				uni.switchTab({
					url: `/pages/message/index`,
				})
			})
		})
	}

	// 处理完成
	function fn3() {
		form.value.$refs.refMyForm.validate().then(val => {
			handleWarnInfo({
				procesName: uni.getStorageSync('userInfo').userName,
				procesTime: date(),
				...data.value
			}).then(res => {
				uni.switchTab({
					url: `/pages/message/index`,
				})
			})
		})
	}
</script>

<style lang="scss" scoped>
	.button {
		position: fixed;
		width: 90%;
		bottom: 40rpx;
		left: 5%;

		.button-1 {
			color: #fff;
			background: linear-gradient(285deg, #24C68E 4%, #27DC9C 89%);
		}

		.button-2 {
			color: #fff;
			background: linear-gradient(285deg, #3F7EFC 4%, #59A6FB 89%);
		}
	}

	:deep() {
		uni-button:after {
			border: 0
		}
	}
</style>